<?PHP
/*
 * $Id: MailAction.class.php 162 2009-12-16 16:03:27Z namba0219 $
 * =============================================================================
 * Barge - Web application framework on PHP5
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * =============================================================================
 * For more information, see http://barge.googlecode.com/
 */
namespace examples\actions;

use \Exception;

use barge\System;
use barge\io\File;
use barge\mail\Mailer;
use barge\mail\TextMessage;
use barge\mail\MultipartBody;
use barge\utils\ArrayList;
use barge\utils\HashMap;
use barge\utils\FileUtils;
use barge\utils\OutputBuffer;
use barge\utils\String;
use barge\utils\Timestamp;
use barge\web\ActionContext;
use barge\web\DispatchAction;
use barge\web\Request;
use barge\web\Response;
use barge\web\Messages;
use barge\web\Message;
use barge\web\Validator;

/**
 * @version 1.0 2009/01/24
 * @author Kenji Namba
 * @see DispatchAction
 */
class MailAction extends DispatchAction
{
	/**
	 * @param ActionContext $context
	 * @see Action#execute()
	 */
	public function init(ActionContext $context)
	{
		/* @var $form MailForm */
		$form = $context->getForm();
		$form->setMailType('text');
		$context->render();
	}

	/**
	 * @param ActionContext $context
	 * @see Action#execute()
	 */
	public function send(ActionContext $context)
	{
		/* @var $form MailForm */
		$form = $context->getForm();

		$mail = new Mailer();
	// ここで設定するか設定レポジトリ上で定義が必要 **************
//		$mail->setHeaderEncoding('ISO-2022-JP');
//		$mail->setHeaderTransferEncoding('7bit');
//		$mail->setTextEncoding('ISO-2022-JP');
//		$mail->setTextTransferEncoding('7bit');
	// ************

		$mail->addTo($form->getTo(), $form->getToName());
		$mail->setFrom($form->getFrom());
		$mail->setSubject($form->getSubject());
		if (System::isNotEmpty($form->getReplyTo()))
			$mail->addReplyTo($form->getReplyTo());
		if (System::isNotEmpty($form->getBcc()))
			$mail->addBcc($form->getBcc());
		if (System::isNotEmpty($form->getCc()))
			$mail->addCc($form->getCc());
		// 本文
		if ($form->getMailType() === 'text')
		{
			$mail->setTextBody($form->getBody());
		}
		else if ($form->getMailType() === 'html')
		{
			$mail->setTextBody($form->getBody(), 'text/html');
		}
		else if ($form->getMailType() === 'althtml')
		{
			$multipartBody = new MultipartBody(MultipartBody::MULTIPART_ALTERNATIVE);
			$multipartBody->addTextBody(strip_tags($form->getBody()).'(transform)', 'text/plain');
			$multipartBody->addTextBody($form->getBody(), 'text/html');
			$mail->setMultipartBody($multipartBody);
		}
		else if ($form->getMailType() === 'attach')
		{
			$multipartBody = new MultipartBody(MultipartBody::MULTIPART_MIXED);
			$multipartBody->addTextBody($form->getBody(), 'text/plain');
			if ($form->getFile1() !== null) {
				$multipartBody->addFileBody($form->getFile1()->getTempFileName(),
						$form->getFile1()->getContentType(),
						$form->getFile1()->getFileName());
			}
			if ($form->getFile2() !== null) {
				$multipartBody->addFileBody($form->getFile2()->getTempFileName(),
						$form->getFile2()->getContentType(),
						$form->getFile2()->getFileName());
			}
			if ($form->getFile3() !== null) {
				$multipartBody->addFileBody($form->getFile3()->getTempFileName(),
						$form->getFile3()->getContentType(),
						$form->getFile3()->getFileName());
			}
			$mail->setMultipartBody($multipartBody);
		}

		$mail->send();


		$context->saveMessage(new Message('GLOBAL', 'messages.sendmail'));
		$context->render();
	}

	/**
	 * @return ArrayList
	 */
	protected function getMethods()
	{
		return new ArrayList('send');
	}
}
